package com.im.dynamic.controller;

import com.alibaba.fastjson.JSON;
import com.im.dynamic.entity.User;
import com.im.dynamic.service.impl.UserLocationService;
import com.im.dynamic.utils.JWTUtils;
import com.im.dynamic.vo.UserLocation;
import org.springframework.data.geo.GeoResults;
import org.springframework.data.redis.connection.RedisGeoCommands;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

/**
 * User: key
 * ClassName:UserLocationController
 * Date: 2023/7/13
 * Time: 16:20
 * description:
 */
@RestController
public class UserLocationController {
    @Resource
    private UserLocationService userLocationService;

    /**
     *添加附近位置
     * */
    @PostMapping("dynamic/addLocation")
    public void addUserLocation(@RequestBody UserLocation userLocation) {
        userLocationService.addUserLocation(userLocation);
    }

    /**
     * 获取附近位置的动态
     * */
    @PostMapping("dynamic/nearby")
    public GeoResults<RedisGeoCommands.GeoLocation<String>> getNearbyUsers(
            @RequestParam double longitude,
            @RequestParam double latitude,
            @RequestParam double radius) {
        //这里id从JWT中获取
        String userInfo = JWTUtils.getUserInfo("userInfo");
        User parse = (User) JSON.parse(userInfo);
        Long id = parse.getId();

        UserLocation location = new UserLocation(longitude, latitude, Math.toIntExact(id));
        return userLocationService.getNearbyUsers(location, radius);
    }
}
